function [ber, numBits] = testing_rs(EbNo, maxNumErrs, maxNumBits)

% Import Java class of BERTool
import com.mathworks.toolbox.comm.BERTool;

% number of bits per iteration
bitsPerIter = 159;
n = 255;
codeRate = 159/255;
m = 8; 
b= ceil(log2(m));
k=bitsPerIter;

% Adjust SNR for coded bits and multi-bit symbols.
adjSNR = EbNo - 10*log10(1/codeRate) + 10*log10(b);

% Initialize the bit error rate and error counters.
totErr = 0;
numBits = 0;

% Exit loop when either the number of bit errors exceeds 'maxNumErrs'
% or the maximum number of iterations have completed
while((totErr < maxNumErrs) && (numBits < maxNumBits))

    % Check if the user has clicked the Stop button of BERTool
    if (BERTool.getSimulationStop)
        break;
    end

    msg_orig = randint(1, bitsPerIter, 256);
    mi = m2r(de2bi(msg_orig,8,'left-msb'));
    % Reed Solomon encoding the message, saving encoder state between iterations
    msg  = gf(msg_orig,m);      % Two k-symbol message words
    msg_enc_bi = rsenc(msg,n,k);

    msg_tx = m2r(de2bi(msg_enc_bi.x,8,'left-msb'));
%     
%     % Demodulation, either OOK or M-PPM
%     msg_ook = ook_mod(msg_tx,10);    
%     
%     % Add Lognormal fading to the signal
%     msg_tx1=apply_lognormal(0.01,msg_ook);
%         
%     % Add Gaussian noise to the signal.
%     msg_rx = awgn(msg_tx1, adjSNR, 'measured', [], 'dB');
% 
%     % Demodulation, OOK
%     msg_ook_demod = ook_demod(msg_rx,10);
%     mx = r2m(msg_ook_demod);

    % Use this set pf codes for 8-PPM or M-PPM
    msg_ppm = ppm_mod(4,msg_tx);
    msg_tx1=apply_lognormal(0.001,msg_ppm);   
    msg_rx = awgn(msg_tx1, adjSNR, 'measured', [], 'dB');
    msg_ppm_demod = ppm_demod(4, msg_rx); 
     for i = 1:2040
        if msg_ppm_demod(i)>1
        msg_ppm_demod(i) = 1
        end
    end
    mx = r2m(msg_ppm_demod);

    msg_dec = rsdec(gf(mx,8),n,k);
    mo = m2r(de2bi(msg_dec.x,8,'left-msb'));
    % Accumulate bit count and bit error statistics after each iteration
    berror = biterr(mi,mo);
    
    % Accumulate bit count and bit error statistics after each iteration
    totErr = totErr + berror;
    numBits = numBits + length(mo);

end

% Compute the BER for all the iterations together
ber = totErr / numBits;